package jgl.vajra.kingkong.part.mine.activity;

import android.content.Intent;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.LoginInfoBean;
import jgl.vajra.kingkong.databinding.ActivityPersonalInformationBinding;
import jgl.vajra.kingkong.event.MapPointEvent;
import jgl.vajra.kingkong.event.ModifyInformationEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.mine.contract.PersonalInformationContract;
import jgl.vajra.kingkong.part.mine.viewModel.PersonalInformationViewModel;
import jgl.vajra.kingkong.widget.bean.ProfileFileBean;
import jgl.vajra.kingkong.widget.giide.GlideImageLoader;
import jgl.vajra.kingkong.widget.interfaces.ImagePickerListener;
import jgl.vajra.kingkong.widget.interfaces.UploadListener;
import jgl.vajra.kingkong.widget.manager.UploadFileManager;
import jgl.vajra.kingkong.widget.manager.UploadFileMapManager;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.utils.PickFileToolsV2;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(PersonalInformationViewModel.class)
public class PersonalInformationActivity extends BaseMVVMActivity<PersonalInformationViewModel, ActivityPersonalInformationBinding> implements PersonalInformationContract.View {

    private PickFileToolsV2 pickFileTools;
    private String avatarUrl;
    private LoginInfoBean data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_information;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getMineData(map);
    }

    //头像
    @Override
    public void onHeadPortrait() {
        if (pickFileTools == null)
            pickFileTools = PickFileToolsV2.init(this);
        pickFileTools.pick(true, 0, new ImagePickerListener() {
            @Override
            public void onPickImageList(List<String> imgs, int tag) {
                super.onPickImageList(imgs, tag);
                avatarUrl = imgs.get(0);

                Map<String, Object> map = new HashMap<>();
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("tag", 3);

                UploadFileMapManager.getInstance().uploadPathMap(map, avatarUrl, "", new UploadListener() {
                    @Override
                    public void onUploadSuccess(List<ProfileFileBean> file) {

                    }

                    @Override
                    public void onUploadStringSuccess(List<String> file) {
                        GlideImageLoader.onAvatarDisplayRound(mBinding.ivAvatar, avatarUrl, null);
                        EventBus.getDefault().post(new ModifyInformationEvent(2,""));
                    }

                    @Override
                    public void onUploadFailed() {

                    }
                });
            }
        });
    }

    //江湖名号
    @Override
    public void onName() {
        IntentManager.toNicknamePageActivity(this, data.nickname);
    }

    //实名认证
    @Override
    public void onNameAuthentication() {
        IntentManager.toNameAuthenticationActivity(this, data.name, data.idCardNumber);
    }

    @Override
    public void returnMineData(LoginInfoBean data) {
        this.data = data;
        mBinding.setData(data);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pickFileTools != null) {
            pickFileTools.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onModifyInformationEvent(ModifyInformationEvent event) {
        if (!TextUtils.isEmpty(event.nickname))
            mBinding.tvNickname.setText(event.nickname);
    }
}
